package scau.zxck.entity;

import scau.zxck.utils.DatabaseUtil;
import scau.zxck.view.Main;
import scau.zxck.view.stage.AlertStage;

import java.util.ArrayList;

/**
 * Created by chenyulin on 28/11/2016.
 */
public class Connection {
    private String connName;
    private String host;
    private String userName;
    private String password;
    private String port;
    private boolean isConnected;
    private ArrayList<String> databaseList;

    public Connection(String connName, String host, String userName, String password, String port) {
        this.connName = connName;
        this.host = host;
        this.userName = userName;
        this.password = password;
        this.port = port;
        this.databaseList = new ArrayList<String>();
    }

    public String getConnName() {
        return connName;
    }

    public void setConnName(String connName) {
        this.connName = connName;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public boolean isConnected() {
        return isConnected;
    }

    public void setConnected(boolean connected) {
        isConnected = connected;
    }

    public ArrayList<String> getDatabaseList() {
        return databaseList;
    }

    public void initDatabaseList() {
        try{
            Class.forName("com.mysql.jdbc.Driver");
            int index = Main.connTreeController.getSelectedItem().getConnIndex();
            for (String dbName : DatabaseUtil.showDatabases(Main.connectionList.get(index))) {
                Main.connectionList.get(index).getDatabaseList().add(dbName);
            }
            Main.connectionList.get(index).setConnected(true);
        } catch (Exception e) {
            new AlertStage("Failed to connect").show();
            this.setConnected(false);
        }
    }
}
